package tv.danmaku.ijk.media.player;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.MediaDataSource;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.view.Surface;
import android.view.SurfaceHolder;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Map;

import tv.danmaku.ijk.media.player.misc.AndroidTrackInfo;
import tv.danmaku.ijk.media.player.misc.IMediaDataSource;
import tv.danmaku.ijk.media.player.misc.ITrackInfo;
import tv.danmaku.ijk.media.player.pragma.DebugLog;

public class AndroidMediaPlayer extends AbstractMediaPlayer {
    class AndroidMediaPlayerListenerHolder implements MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnInfoListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnSeekCompleteListener, MediaPlayer.OnVideoSizeChangedListener {
        public final WeakReference mWeakMediaPlayer;

        public AndroidMediaPlayerListenerHolder(AndroidMediaPlayer arg2, AndroidMediaPlayer arg3) {
            this.mWeakMediaPlayer = new WeakReference(arg3);
        }

        public void onBufferingUpdate(MediaPlayer arg2, int arg3) {
            if (this.mWeakMediaPlayer.get() != null) {
                AndroidMediaPlayer.this.notifyOnBufferingUpdate(arg3);
            }
        }

        public void onCompletion(MediaPlayer arg2) {
            if (this.mWeakMediaPlayer.get() != null) {
                AndroidMediaPlayer.this.notifyOnCompletion();
            }
        }

        public boolean onError(MediaPlayer arg2, int arg3, int arg4) {
            boolean v0 = this.mWeakMediaPlayer.get() == null || !AndroidMediaPlayer.this.notifyOnError(arg3, arg4) ? false : true;
            return v0;
        }

        public boolean onInfo(MediaPlayer arg2, int arg3, int arg4) {
            boolean v0 = this.mWeakMediaPlayer.get() == null || !AndroidMediaPlayer.this.notifyOnInfo(arg3, arg4) ? false : true;
            return v0;
        }

        public void onPrepared(MediaPlayer arg2) {
            if (this.mWeakMediaPlayer.get() != null) {
                AndroidMediaPlayer.this.notifyOnPrepared();
            }
        }

        public void onSeekComplete(MediaPlayer arg2) {
            if (this.mWeakMediaPlayer.get() != null) {
                AndroidMediaPlayer.this.notifyOnSeekComplete();
            }
        }

        public void onVideoSizeChanged(MediaPlayer arg3, int arg4, int arg5) {
            if (this.mWeakMediaPlayer.get() != null) {
                AndroidMediaPlayer.this.notifyOnVideoSizeChanged(arg4, arg5, 1, 1);
            }
        }
    }

    @TargetApi(value = 23)
    class MediaDataSourceProxy extends MediaDataSource {
        private final IMediaDataSource mMediaDataSource;

        public MediaDataSourceProxy(IMediaDataSource arg1) {

            this.mMediaDataSource = arg1;
        }

        public void close() throws IOException {
            this.mMediaDataSource.close();
        }

        public long getSize() throws IOException {
            return this.mMediaDataSource.getSize();
        }

        public int readAt(long arg8, byte[] arg10, int arg11, int arg12) throws IOException {
            return this.mMediaDataSource.readAt(arg8, arg10, arg11, arg12);
        }
    }

    private String mDataSource;
    private final Object mInitLock;
    private final AndroidMediaPlayerListenerHolder mInternalListenerAdapter;
    private final MediaPlayer mInternalMediaPlayer;
    private boolean mIsReleased;
    private MediaDataSource mMediaDataSource;
    private static MediaInfo sMediaInfo;

    public AndroidMediaPlayer() {
        this.mInitLock = new Object();
        synchronized (mInitLock) {
            this.mInternalMediaPlayer = new MediaPlayer();
        }
        this.mInternalMediaPlayer.setAudioStreamType(3);
        this.mInternalListenerAdapter = new AndroidMediaPlayerListenerHolder(this, this);
        this.attachInternalListeners();
    }

    private void attachInternalListeners() {
        this.mInternalMediaPlayer.setOnPreparedListener(this.mInternalListenerAdapter);
        this.mInternalMediaPlayer.setOnBufferingUpdateListener(this.mInternalListenerAdapter);
        this.mInternalMediaPlayer.setOnCompletionListener(this.mInternalListenerAdapter);
        this.mInternalMediaPlayer.setOnSeekCompleteListener(this.mInternalListenerAdapter);
        this.mInternalMediaPlayer.setOnVideoSizeChangedListener(this.mInternalListenerAdapter);
        this.mInternalMediaPlayer.setOnErrorListener(this.mInternalListenerAdapter);
        this.mInternalMediaPlayer.setOnInfoListener(this.mInternalListenerAdapter);
    }

    public int getAudioSessionId() {
        return this.mInternalMediaPlayer.getAudioSessionId();
    }

    public long getCurrentPosition() {
        long currentPosition;
        try {
            currentPosition = this.mInternalMediaPlayer.getCurrentPosition();
        } catch (IllegalStateException illegalStateException) {
            DebugLog.printStackTrace(illegalStateException);
            currentPosition = 0;
        }
        return currentPosition;
    }

    public String getDataSource() {
        return this.mDataSource;
    }

    public long getDuration() {
        long duration;
        try {
            duration = (long) this.mInternalMediaPlayer.getDuration();
        } catch (IllegalStateException illegalStateException) {
            DebugLog.printStackTrace(illegalStateException);
            duration = 0;
        }
        return duration;
    }

    public MediaPlayer getInternalMediaPlayer() {
        return this.mInternalMediaPlayer;
    }

    public MediaInfo getMediaInfo() {
        if (AndroidMediaPlayer.sMediaInfo == null) {
            MediaInfo mediaInfo = new MediaInfo();
            mediaInfo.mVideoDecoder = "android";
            mediaInfo.mVideoDecoderImpl = "HW";
            mediaInfo.mAudioDecoder = "android";
            mediaInfo.mAudioDecoderImpl = "HW";
            AndroidMediaPlayer.sMediaInfo = mediaInfo;
        }
        return AndroidMediaPlayer.sMediaInfo;
    }

    public ITrackInfo[] getTrackInfo() {
        return AndroidTrackInfo.fromMediaPlayer(this.mInternalMediaPlayer);
    }

    public int getVideoHeight() {
        return this.mInternalMediaPlayer.getVideoHeight();
    }

    public int getVideoSarDen() {
        return 1;
    }

    public int getVideoSarNum() {
        return 1;
    }

    public int getVideoWidth() {
        return this.mInternalMediaPlayer.getVideoWidth();
    }

    public boolean isLooping() {
        return this.mInternalMediaPlayer.isLooping();
    }

    public boolean isPlayable() {
        return true;
    }

    public boolean isPlaying() {
        boolean result;
        try {
            result = this.mInternalMediaPlayer.isPlaying();
        } catch (IllegalStateException illegalStateException) {
            DebugLog.printStackTrace(illegalStateException);
            result = false;
        }
        return result;
    }

    public void pause() throws IllegalStateException {
        this.mInternalMediaPlayer.pause();
    }

    public void prepareAsync() throws IllegalStateException {
        this.mInternalMediaPlayer.prepareAsync();
    }

    public void release() {
        this.mIsReleased = true;
        this.mInternalMediaPlayer.release();
        this.releaseMediaDataSource();
        this.resetListeners();
        this.attachInternalListeners();
    }

    private void releaseMediaDataSource() {
        if (this.mMediaDataSource != null) {
            try {
                this.mMediaDataSource.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            this.mMediaDataSource = null;
        }
    }

    public void reset() {
        try {
            this.mInternalMediaPlayer.reset();
        } catch (IllegalStateException illegalStateException) {
            DebugLog.printStackTrace(illegalStateException);
        }
        this.releaseMediaDataSource();
        this.resetListeners();
        this.attachInternalListeners();
    }

    public void seekTo(long msec) throws IllegalStateException {
        this.mInternalMediaPlayer.seekTo(((int) msec));
    }

    public void setAudioStreamType(int streamType) {
        this.mInternalMediaPlayer.setAudioStreamType(streamType);
    }

    public void setDataSource(Context context, Uri uri) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
        this.mInternalMediaPlayer.setDataSource(context, uri);
    }

    @TargetApi(value = 14)
    public void setDataSource(Context context, Uri uri, Map headers) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
        this.mInternalMediaPlayer.setDataSource(context, uri, headers);
    }

    public void setDataSource(FileDescriptor fd) throws IOException, IllegalArgumentException, IllegalStateException {
        this.mInternalMediaPlayer.setDataSource(fd);
    }

    public void setDataSource(String path) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
        this.mDataSource = path;
        Uri uri = Uri.parse(path);
        String scheme = uri.getScheme();
        if ((TextUtils.isEmpty(scheme)) || !scheme.equalsIgnoreCase("file")) {
            this.mInternalMediaPlayer.setDataSource(path);
        } else {
            this.mInternalMediaPlayer.setDataSource(uri.getPath());
        }
    }

    @TargetApi(value = 23)
    public void setDataSource(IMediaDataSource mediaDataSource) {
        this.releaseMediaDataSource();
        this.mMediaDataSource = new MediaDataSourceProxy(mediaDataSource);
        this.mInternalMediaPlayer.setDataSource(this.mMediaDataSource);
    }

    public void setDisplay(SurfaceHolder surfaceHolder) {
        synchronized (mInitLock) {
            if (!this.mIsReleased) {
                this.mInternalMediaPlayer.setDisplay(surfaceHolder);
            }
        }
    }

    public void setKeepInBackground(boolean keepInBackground) {
    }

    public void setLogEnabled(boolean enable) {
    }

    public void setLooping(boolean looping) {
        this.mInternalMediaPlayer.setLooping(looping);
    }

    public void setScreenOnWhilePlaying(boolean screenOn) {
        this.mInternalMediaPlayer.setScreenOnWhilePlaying(screenOn);
    }

    @TargetApi(value = 14)
    public void setSurface(Surface surface) {
        this.mInternalMediaPlayer.setSurface(surface);
    }

    public void setVolume(float leftVolume, float rightVolume) {
        this.mInternalMediaPlayer.setVolume(leftVolume, rightVolume);
    }

    public void setWakeMode(Context context, int mode) {
        this.mInternalMediaPlayer.setWakeMode(context, mode);
    }

    public void start() throws IllegalStateException {
        this.mInternalMediaPlayer.start();
    }

    public void stop() throws IllegalStateException {
        this.mInternalMediaPlayer.stop();
    }
}

